//
//  BCVersionInfoController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/16.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCVersionInfoController.h"
//cell、header、footer
#import "BCApplicationInfoSectionHeaderView.h"

//
#import "BCApplicationInfoViewModel+Source.h"

@interface BCVersionInfoController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *appTable;


/**
 *dataSource
 */
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation BCVersionInfoController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
/**
 加载显示数据
 */
- (void)loadDataSource {
    [self.dataSource removeAllObjects];
    //版本信息
    BCApplicationInfoViewModel *versionModel = [BCApplicationInfoViewModel versionInfo];
    if (versionModel) {
        //icon版本信息
        BCApplicationInfoCellModel *versionCellM = [BCApplicationInfoViewModel versionCellModel];
        [versionModel.sectionSource addObject:versionCellM];
        //版本更新
        BCApplicationInfoCellModel *update = [BCApplicationInfoViewModel versionUpdate];
        [versionModel.sectionSource addObject:update];
        
        if (versionModel.sectionSource.count) {
            [_dataSource addObject:versionModel];
        }
    }
    //设备信息
    BCApplicationInfoViewModel *deviceModel = [BCApplicationInfoViewModel deviceInfo];
    if (deviceModel) {
        //alias
        BCApplicationInfoCellModel *alias = [BCApplicationInfoViewModel deviceAlias];
        [deviceModel.sectionSource addObject:alias];
        //sys
        BCApplicationInfoCellModel *system = [BCApplicationInfoViewModel deviceSystem];
        [deviceModel.sectionSource addObject:system];
        //model
        BCApplicationInfoCellModel *model = [BCApplicationInfoViewModel deviceModel];
        [deviceModel.sectionSource addObject:model];
        //uuid
        BCApplicationInfoCellModel *uuid = [BCApplicationInfoViewModel deviceUUID];
        [deviceModel.sectionSource addObject:uuid];
        //
        if (deviceModel.sectionSource.count) {
            [_dataSource addObject:deviceModel];
        }
    }
}



//
- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.title = BCLocalizedKey(@"Application Infomation");
    //
    [self loadDataSource];
    //
    [self setupSubviews];
}

- (void)setupSubviews {
    self.appTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.appTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.appTable.backgroundColor = [UIColor separatorColor];
    [self.view addSubview:self.appTable];
    [self.appTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    //
    [self.view layoutIfNeeded];
    //
    [self.appTable registerClass:BCApplicationInfoSectionHeaderView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(BCApplicationInfoSectionHeaderView.class)];
    //cells
    NSMutableArray *registerCells = [BCApplicationInfoViewModel registerCells];
    for (NSString *cellName in registerCells) {
        [self.appTable registerClass:NSClassFromString(cellName) forCellReuseIdentifier:cellName];
    }
    //
    self.appTable.dataSource = self;
    self.appTable.delegate = self;
    //
    UIView *topHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10.0)];
    [self.appTable setTableHeaderView:topHeader];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
//section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BCApplicationInfoSectionHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(BCApplicationInfoSectionHeaderView.class)];
    header.viewModel = self.dataSource[section];
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [BCApplicationInfoSectionHeaderView defaultHeight];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

//cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    BCApplicationInfoViewModel *viewModel = self.dataSource[section];
    return viewModel.sectionSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCApplicationInfoViewModel *viewModel = self.dataSource[indexPath.section];
    BCApplicationInfoCellModel *cellModel = viewModel.sectionSource[indexPath.row];
    BCApplicationBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellModel.cellClass)];
    cell.cellModel = cellModel;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    BCApplicationInfoViewModel *viewModel = self.dataSource[indexPath.section];
    BCApplicationInfoCellModel *cellModel = viewModel.sectionSource[indexPath.row];
    if (cellModel.cellClass==BCApplicationVersionIconCell.class) {
        return [BCApplicationVersionIconCell defaultHeight];
    }
    return [BCApplicationBaseCell defaultHeight];
}
//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BCApplicationInfoViewModel *viewModel = self.dataSource[indexPath.section];
    BCApplicationInfoCellModel *cellModel = viewModel.sectionSource[indexPath.row];
    [self tableViewDidSelect:cellModel];
}
- (void)tableViewDidSelect:(BCApplicationInfoCellModel *)model {
    switch (model.cellType) {
        case BCApplicationInfoCellTypeVersionUpdate://版本更新
        {
            [self toAppStoreUpdateVersionAction];
        }
            break;
        default:
            break;
    }
}

#pragma mark - Do Actions
/**
 去appStore更新
 */
- (void)toAppStoreUpdateVersionAction {
    NSString *updateURLString = AppStoreConnectURLString;
    NSURL *url = [NSURL URLWithString:updateURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        if (@available(iOS 10.0,*)) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }else {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

@end
